package com. zyq.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyq.exception.CustomException;
import com.zyq.model.common.ResponseBean;
import com.zyq.model.entity.HealthInformation;
import com.zyq.model.request.HealthInformationDto;
import com.zyq.service.IHealthInformationService;
import com.zyq.service.IUserService;
import com.zyq.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 健康信息表Controller
 *
 * @author zyq
 * @date 2021-12-08
 */
@Api( tags = "健康信息表接口" )
@RestController
@RequestMapping("/healthInformation")
public class HealthInformationController
{
    private final UserUtil userUtil;

    private final IHealthInformationService healthInformationService;

    @Autowired
    public HealthInformationController(UserUtil userUtil, IHealthInformationService healthInformationService) {
        this.userUtil = userUtil;
        this.healthInformationService = healthInformationService;
    }

//    private UserUtil userUtil;

//    @Autowired
//    private IHealthInformationService healthInformationService;

    /**
     * 查询健康信息表列表
     */
    @ApiOperation("查询健康信息表列表")
    @RequiresRoles("管理员")
    @GetMapping("/list")
    public ResponseBean list(HealthInformationDto healthInformation, @RequestHeader String Authorization)
    {

        PageHelper.startPage(healthInformation.getPageNum(), healthInformation.getPageSize());
        List<HealthInformation> list = healthInformationService.selectHealthInformationList(healthInformation.getHealthInformation());
        PageInfo<HealthInformation> selectPage = new PageInfo<>(list);
        if (list == null || list.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("total", selectPage.getTotal());
        result.put("pages", selectPage.getPages());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    /**
     * 查询用户自己的健康信息表列表
     */
    @ApiOperation("查询用户自己的健康信息表列表")
    @GetMapping("/detail")
    @RequiresAuthentication
    public ResponseBean detail(@RequestHeader String Authorization)
    {
        HealthInformation healthInformation = healthInformationService.selectHealthInformationByMyself(userUtil.getUserId());
        return new ResponseBean(HttpStatus.OK.value(),
                "查询成功(Query was successful)",
                healthInformationService.selectHealthInformationByMyself(userUtil.getUserId()));
    }

    /**
     * 获取健康信息表详细信息
     */
    @ApiOperation("获取健康信息表详细信息")
    @GetMapping(value = "/{id}")
    public ResponseBean getInfo(@PathVariable("id") Long id)
    {
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("data", healthInformationService.selectHealthInformationById(id));
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    /**
     * 新增健康信息表
     */
    @ApiOperation("新增健康信息表")
    @PostMapping
    public ResponseBean add(@RequestBody HealthInformation healthInformation)
    {
        if(healthInformationService.insertHealthInformation(healthInformation)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    /**
     * 修改健康信息表
     */
    @ApiOperation("修改健康信息表")
    @PutMapping
    public ResponseBean edit(@RequestBody HealthInformation healthInformation)
    {
        healthInformation.setUserId(userUtil.getUserId());
        if(healthInformationService.updateHealthInformation(healthInformation)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }else{
            if(healthInformationService.insertHealthInformation(healthInformation)>0){
                return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
            }
            return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
        }
    }

    /**
     * 删除健康信息表
     */
    @ApiOperation("删除健康信息表")
    @DeleteMapping("/{ids}")
    public ResponseBean remove(@PathVariable Long[] ids)
    {
        if(healthInformationService.deleteHealthInformationByIds(ids)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }
}
